if (!window.dhtmlx)
	window.dhtmlx = {};
(function() {
	var _dhx_msg_cfg = null;
	function callback(config, result) {
		var usercall = config.callback;
		modality(false);
		config.box.parentNode.removeChild(config.box);
		_dhx_msg_cfg = box = config.box = null;
		if (usercall)
			usercall(result);
	}
	function modal_key(e) {
		if (_dhx_msg_cfg) {
			var code = e.which || event.keyCode;
			if (dhtmlx.message.keyboard) {
				if (code == 13 || code == 32)
					callback(_dhx_msg_cfg, true);
				if (code == 27)
					callback(_dhx_msg_cfg, false);
			}
			if (e.preventDefault)
				e.preventDefault();
			return !(e.cancelBubble = true);
		}
	};
	if (document.attachEvent)
		document.attachEvent("onkeydown", modal_key);
	else
		document.addEventListener("keydown", modal_key, false);
	function modality(mode) {
		if (!modality.cover) {
			modality.cover = document.createElement("DIV");
			modality.cover.className = "dhx_modal_cover";
			document.body.appendChild(modality.cover);
		}
		var height = document.body.scrollHeight;
		modality.cover.style.display = mode ? "inline-block" : "none";
	}
	function button(text, callback, callback_special) {
		return "<div class='dhtmlx_popup_button' "
        + (callback_special||false ? "result='" + callback + "' " : "")
				+ (!(callback_special||false) && callback ? "result='true' " : "") + "><div>" + text
				+ "</div></div>";
	}
	function info(text) {
		if (!t.area) {
			t.area = document.createElement("DIV");
			t.area.className = "dhtmlx_message_area";
			t.area.style[t.position] = "5px";
			document.body.appendChild(t.area);
		}
		t.hide(text.id);
		var message = document.createElement("DIV");
		message.innerHTML = "<div>" + text.text + "</div>";
		message.className = "dhtmlx-info dhtmlx-" + text.type;
		message.onclick = function() {
			t.hide(text.id);
			text = null;
		};
		if (t.position == "bottom" && t.area.firstChild)
			t.area.insertBefore(message, t.area.firstChild);
		else
			t.area.appendChild(message);
		if (text.expire > 0)
			t.timers[text.id] = window.setTimeout(function() {
						t.hide(text.id);
					}, text.expire);
		t.pull[text.id] = message;
		message = null;
		return text.id;
	}
	function _createBox(config, ok, cancel) {
		var box = document.createElement("DIV");
		box.className = " dhtmlx_modal_box dhtmlx-" + config.type;
		var inner = '';
		if (config.title)
			inner += '<div class="dhtmlx_popup_title">' + config.title
					+ '</div>';
		inner += '<div class="dhtmlx_popup_text"><span>' + config.text
				+ '</span></div><div  class="dhtmlx_popup_controls">';
    
    if(typeof(config.buttons) != 'undefined') {
      for (var property in config.buttons) {
        if (config.buttons.hasOwnProperty(property)) {
          inner += button(config.buttons[property], property, true);
        }
      }
    }
    else {
      if (ok)
  			inner += button(config.ok || "OK", true);
  		if (cancel)
  			inner += button(config.cancel || "Cancel", false);
    }
		inner += '</div>';
		box.innerHTML = inner;
		box.onclick = function(e) {
			e = e || event;
			var source = e.target || e.srcElement;
			if (!source.className)
				source = source.parentNode;
			if (source.className == "dhtmlx_popup_button") {
        if(typeof(config.buttons) != 'undefined')
          callback(config, source.getAttribute("result"));
        else
          callback(config, source.getAttribute("result") == "true");
      }
		};
		config.box = box;
		_dhx_msg_cfg = config;
		modality(true, box);
		document.body.appendChild(box);
		var x = Math
				.abs(Math
						.floor(((window.innerWidth || document.documentElement.offsetWidth) - box.offsetWidth)
								/ 2));
		var y = Math
				.abs(Math
						.floor(((window.innerHeight || document.documentElement.offsetHeight) - box.offsetHeight)
								/ 2));
		box.style.top = y + 'px';
		box.style.left = x + 'px';
		box.focus();
	}
	function _popupButtonClick(config, param) {
		return function() {
			t.hide(config.id);
			if (typeof config.callback == "function")
				config.callback(param);
		};
	}
	function alertPopup(config) {
		var box = _createBox(config, true, false);
	}
	function confirmPopup(config) {
		var box = _createBox(config, true, true);
	}
	function box_params(text, type, callback) {
		if (typeof text != "object") {
			if (typeof type == "function") {
				callback = type;
				type = "";
			}
			text = {
				text : text,
				type : type,
				callback : callback
			};
		}
		return text;
	}
	function params(text, type, expire, id) {
		if (typeof text != "object")
			text = {
				text : text,
				type : type,
				expire : expire,
				id : id
			};
		text.id = text.id || t.uid();
		text.expire = text.expire || t.expire;
		return text;
	}
	dhtmlx.alert = function() {
		text = box_params.apply(this, arguments);
		text.type = text.type || "confirm";
		alertPopup(text);
	};
	dhtmlx.confirm = function() {
		text = box_params.apply(this, arguments);
		text.type = text.type || "alert";
		confirmPopup(text);
	};
	var t = dhtmlx.message = function(text, type, expire, id) {
		text = params.apply(this, arguments);
		text.type = text.type || "info";
		var subtype = text.type.split("-")[0];
		switch (subtype) {
			case "alert" :
				return alertPopup(text);
				break;
			case "confirm" :
				return confirmPopup(text);
				break;
			default :
				return info(text);
				break;
		}
	};
	t.seed = (new Date()).valueOf();
	t.uid = function() {
		return t.seed++;
	};
	t.expire = 4000;
	t.keyboard = true;
	t.position = "top";
	t.pull = {};
	t.timers = {};
	t.hideAll = function() {
		for (var key in t.pull)
			t.hide(key);
	};
	t.hide = function(id) {
		var obj = t.pull[id];
		if (obj && obj.parentNode) {
			window.setTimeout(function() {
						obj.parentNode.removeChild(obj);
						obj = null;
					}, 2000);
			obj.className += " hidden";
			if (t.timers[id])
				window.clearTimeout(t.timers[id]);
			delete t.pull[id];
		}
	};
})();